package com.vxdata.utils.modules.pdf.jacob;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.File;


/**
 * @ClassName Word2PdfService
 * @Description
 * @Author cutepig
 * @Date 2021/7/17 14:31
 * @Version 1.0
 */
@Service
@Lazy(value = true)
public class Word2PdfService {

    @Value("${word2pdf.template.path:}")
    private String pdfTemplatePath;

    @Value("${word2pdf.temp.path:}")
    private String pdfTempPath;

    public String getPdfTemplatePath() {
        return pdfTemplatePath;
    }

    public void setPdfTemplatePath(String pdfTemplatePath) {
        this.pdfTemplatePath = pdfTemplatePath;
    }

    public String getPdfTempPath() {
        return pdfTempPath;
    }

    public void setPdfTempPath(String pdfTempPath) {
        this.pdfTempPath = pdfTempPath;
    }

    Logger logger = LoggerFactory.getLogger(getClass());
    // 不保存待定的更改。
    static final int wdDoNotSaveChanges = 0;
    // PDF 格式
    static final int wdFormatPDF = 17;

    public boolean wordToPdf(String wordpath, String pdfpath) {
        boolean bRet = false;
        logger.info("启动Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        try {
            ComThread.InitMTA(true);
            // 打开word应用程序
            // app = new ActiveXComponent("Word.Application");
            app = ActiveXComponent.createNewInstance("Word.Application");
            //// 设置应用操作是文档不在明面上显示，只在后台静默处理。
            app.setProperty("Visible", false);
            // 获得文档集合，用来操作我们需要处理的文档.
            Dispatch docs = app.getProperty("Documents").toDispatch();
            logger.info("打开文档..." + wordpath);
            // 打开word文档
            Dispatch doc = Dispatch.call(docs, //
                    "Open", //
                    wordpath, // FileName
                    false, // ConfirmConversions
                    true // ReadOnly
            ).toDispatch();

            logger.info("转换文档到PDF..." + pdfpath);
            File tofile = new File(pdfpath);
            // 创建存放pdf的文件夹
            if (tofile.exists()) {
                tofile.delete();
            }
            // 将word另存为pdf
            Dispatch.call(doc,
                    "SaveAs",
                    pdfpath,
                    wdFormatPDF);
            // 关闭word文档
            Dispatch.call(doc, "Close", false);

            logger.info("转换文档到PDF..." + pdfpath);
            bRet = true;
        } catch (Exception e) {
            logger.error("========Error:文档转换失败：" + e.getMessage());
        } finally {
            try {
                if (app != null) {
                    app.invoke("Quit", wdDoNotSaveChanges);
                    app.safeRelease();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            ComThread.Release();
        }
        long end = System.currentTimeMillis();
        logger.info("word转pdf耗时:" + (end - start) + "ms");
        return bRet;
    }

}
